#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QHostAddress>
#include <QJsonArray>
#include <QTimer>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    tcpSocket=new QTcpSocket(this);
    tcpSocket->connectToHost("192.168.10.142", 6002);
    connect(tcpSocket, qOverload<QAbstractSocket::SocketError>(&QTcpSocket::error), this, [=](QAbstractSocket::SocketError error){
        qDebug() << "error" << error << ":" << tcpSocket->errorString();
    });

}

Widget::~Widget()
{
    delete ui;
}

QByteArray Widget::getSendJsondata(QJsonObject obj)
{
    QByteArray headTemprature;
    headTemprature.resize(11);
    headTemprature[0] = '\xf4';headTemprature[1] = '\xa3';
    QByteArray sendDataglobalConfig = QJsonDocument(obj).toJson();
    headTemprature[2] = (uint8_t)(((sendDataglobalConfig.size() + 11) & 0xff000000) >> 24);
    headTemprature[3] = (uint8_t)(((sendDataglobalConfig.size() + 11) & 0x00ff0000) >> 16);
    headTemprature[4] = (uint8_t)(((sendDataglobalConfig.size() + 11) & 0x0000ff00) >> 8);
    headTemprature[5] = (uint8_t)((sendDataglobalConfig.size() + 11) & 0x000000ff);
    headTemprature[6] = 1;
    headTemprature[7] = (uint8_t)(((sendDataglobalConfig.size() + 5) & 0xff000000) >> 24);
    headTemprature[8] = (uint8_t)(((sendDataglobalConfig.size() + 5) & 0x00ff0000) >> 16);
    headTemprature[9] = (uint8_t)(((sendDataglobalConfig.size() + 5) & 0x0000ff00) >> 8);
    headTemprature[10] = (uint8_t)(((sendDataglobalConfig.size() + 5) & 0x000000ff));
    sendDataglobalConfig  = headTemprature + sendDataglobalConfig;
    qDebug()<<"wt"<<sendDataglobalConfig;
    return sendDataglobalConfig;
}

void Widget::on_pushButton_clicked()
{
    // //GPO
    // QJsonObject rootObject;
    // rootObject["major"] = 1;
    // rootObject["minor"] = 115;
    // QJsonObject valueObject;
    // valueObject["GPO0"] = 0;
    // valueObject["GPO1"] = 1;
    // valueObject["GPO2"] = 0;
    // valueObject["GPO3"] = 1;
    // rootObject["value"] = valueObject;
    // qint64 bytesWritten = tcpSocket->write(getSendJsondata(rootObject));

    // //停止工作
    // QJsonObject rootObject;
    // rootObject["major"] = 1;
    // rootObject["minor"] = 3;
    // qint64 bytesWritten = tcpSocket->write(getSendJsondata(rootObject));

    //切换方案二(有问题)
    // QJsonObject rootObject;
    // rootObject["major"] = 1;
    // rootObject["minor"] = 78;
    // rootObject["value"] = 1;
    // tcpSocket->write(getSendJsondata(rootObject));

    //删除方案二(有问题)
    // QJsonObject rootObject;
    // rootObject["major"] = 1;
    // rootObject["minor"] = 79;
    // rootObject["value"] = 1;
    // tcpSocket->write(getSendJsondata(rootObject));

    // //清空日志（有问题）
    // QJsonObject rootObject;
    // rootObject["major"] = 1;
    // rootObject["minor"] = 121;
    // tcpSocket->write(getSendJsondata(rootObject));
    // QTimer::singleShot(8000, [&]() {
    //     qDebug()<<"wt123"<<tcpSocket->readAll();

    // });

    QJsonObject rootObject1;
    rootObject1["major"] = 1;
    rootObject1["minor"] = 3;
    tcpSocket->write(getSendJsondata(rootObject1));
    QTimer::singleShot(1000,[this](){
        QJsonObject rootObject2;
        rootObject2["major"] = 1;
        rootObject2["minor"] = 15;
        rootObject2["value1"] = 2;
        rootObject2["value2"] = 2;
        tcpSocket->write(getSendJsondata(rootObject2));
    });



}

